package com.wen.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wen.common.Result;
import com.wen.entity.Leave;
import com.wen.service.LeaveService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 请假
 *
 * @author dhh
 */
@RestController
@AllArgsConstructor
@RequestMapping("/leave")
public class LeaveController {

    private final LeaveService leaveService;

    //    添加设备信息
    @PostMapping
    public Result<?> save(@RequestBody Leave leave) {
        leaveService.save(leave);
        return Result.success();
    }

    //    更新
    @PutMapping
    public Result<?> update(@RequestBody Leave leave) {
        leaveService.updateById(leave);
        return Result.success();
    }

    //    s删除
    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id) {
        leaveService.removeById(id);
        return Result.success();
    }

    //    查询
    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search) {
        LambdaQueryWrapper<Leave> wrapper = Wrappers.<Leave>lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(Leave::getCoachName, search);
        }
        Page<Leave> NewsPage = leaveService.page(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(NewsPage);
    }
}
